import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class VolatileDemo {

    //加不加volatile
    static volatile int a = 0;


    public static void main(String[] args) {

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "读取a为" + a);
            while (a == 0) {

            }
            System.out.println("a=" + a);
        }, "t1").start();

        //需要停一秒，要不main跟t1同步，直接读主内存为mian修改后的
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

//        new Thread(() -> {
//            a = 1;
//            System.out.println(Thread.currentThread().getName()+"修改");
//        }, "t2").start();

        a = 1;
        System.out.println(Thread.currentThread().getName() + "修改a为" + a);

    }

}
